package personal.vivian.logic;

import groovy.lang.Script;

import java.util.Map;

public class GroovyClass<T> {
    T cls;
    long lastmodified;

    public T getCls() {
        return cls;
    }

    public void setCls(T cls) {
        this.cls = cls;
    }

    public long getLastmodified() {
        return lastmodified;
    }

    public void setLastmodified(long lastmodified) {
        this.lastmodified = lastmodified;
    }
    public  GroovyClass(T cls){
        this.cls = cls;
    }
    public Object getScript(Map<String ,Object> binds){
        if(cls instanceof Script){
            Script script = (Script)cls;
            bindParameter(script,binds);
            Object obj = script.run();
            script=null;
            return obj;
        }
        return cls;
    }

    private void bindParameter(Script script, Map<String, Object> binds) {
        if(binds!=null){
            script.getBinding().getVariables().putAll(binds);
        }
    }
}
